//
// Created by Nc2016 on 2019/1/4.
//

#include "Descriptor.h"
#include "Msp.h"
#include "Devs.h"

static void RccInit(void);

static void NvicInit(void);

static void PerEnable(void);


extern "C"
void Core(void)
{
    RccInit();
    NvicInit();
    PerEnable();
}

static void RccInit()
{
    //Power Set to Scale1
    RCC->APB1Enable(APB1_Power);
    PWR->Scale(Power_Scale1);

    //Set Flash Wait Cycles
    FLASH->WaitCycles(3);


    //Pll Config
    RCC->HseOn();
    RCC->Pll(25, 288, 2, 6);
    RCC->PllOn();

    //System Clock Config
    RCC->Clk(AHB_Clk_Div1, APB_Clk_Div4, APB_Clk_Div2, Clk_Src_Pll);
}

void NvicInit(void)
{
    M4.NvicGroup(Group_4);
    M4.NvicPri(PendSV_IRQn, 15, 0);
    M4.NvicPri(SysTick_IRQn, 10, 0);
    M4.NvicPri(USART1_IRQn, 7, 0);
    M4.NvicPri(TIM5_IRQn, 12, 0);
}

static void PerEnable()
{
    RCC->AHB1Enable(AHB1_GpioA);
    RCC->AHB1Enable(AHB1_GpioB);
    RCC->AHB1Enable(AHB1_GpioC);
    RCC->AHB1Enable(AHB1_GpioD);
    RCC->AHB1Enable(AHB1_GpioE);
    RCC->AHB1Enable(AHB1_DMA2);

    RCC->APB1Enable(APB1_Tim5);
    RCC->APB2Enable(APB2_Usart1);
}






